#/bin/bash

PACKAGES="git vim tig ckermit"
HOMEDIR=`env | grep ^HOME= | cut -c 6-`
DIRS="${HOMEDIR}/workspace/project ${HOMEDIR}/workspace/Documents ${HOMEDIR}/workspace/datasheet ${HOMEDIR}/workspace/hardware ${HOMEDIR}/Git ${HOMEDIR}/github"
BACKUPDIR="./backup"
DEBDIR="${BACKUPDIR}/debs"

AUSTONIOPACKAGES="gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev mingw32 tofrodos python-markdown libxml2-utils libxml2:i386 xsltproc zlib1g-dev:i386 ccache squashfs-tools nasm python-libxml2 git-email gcc-multilib g++-multilib lib32ncurses5-dev connect-proxy dos2unix quilt ImageMagick"
JDK="openjdk-7-jdk"


KERMITSOURCE="${BACKUPDIR}/home/.kermrc"

VIMSOURCE="${BACKUPDIR}/etc/vim/vimrc.local"
VIMDEST="/etc/vim/"

OSCHINA="https://gitee.com/"
AUTHOR="wintonliu"

createdir(){
	for dir in ${DIRS}
	do
		mkdir -p ${dir}
	done
}

copyfile(){
	if [ -f ${1} ];then
		echo "Copying file ${1} to ${2}"
		sudo cp ${1} ${2}
	else
		echo "File ${1} doesn't exist"
	fi
}

installapp(){
	local package=${1}
	echo "Installing Package ${package}"
	sudo apt-get install ${package}
}

installpackage(){
	installapp ${PACKAGES}
}


clonegit(){
	cd ${HOMEDIR}/github
	wget "${OSCHINA}${AUTHOR}" -q -O /tmp/gits
	projects=`cat /tmp/gits | grep "project-title" | awk -F"href=" '{print $2}' | awk -F">" '{print $1}' | sed  's/"//g' | sed 's/\/wintonliu\///g'`
	for project in ${projects}
	do
		echo "${project}"
		git clone ${OSCHINA}${AUTHOR}/${project}
	done
}

installandriod(){
	for package in ${AUSTONIOPACKAGES}
	do
		installapp ${package}
	done
		installapp ${JDK}
}

installkermit(){
	installapp ckermit
	copyfile ${KERMITSOURCE} ${HOMEDIR}
}

installctag(){
	installapp "cscope ctags"
}

installssh(){
	installapp openssh-server
	netstat -tul | grep ssh
	if [ $? -eq 0 ];then
		echo "Install ssh service succeed!"
	else
		echo "Install ssh service failed!"
	fi
}

installdeb(){
	for file in ${DEBDIR}/*.deb
	do
		sudo dpkg -i ${file}
	done
}

installvim(){
	installapp vim
	copyfile ${VIMSOURCE} ${VIMDEST}
}

configuration(){
	git config --global user.email 18502523564@163.com
	git config --global user.name winton.liu
	git config --global core.editor vim
}

createdir
installvim
installssh
installkermit
installctag
installandriod
#installdeb
clonegit
